package me.shurufa.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.fragments.CatalogFragment;
import me.shurufa.model.BookInfoResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.LogUtils;

/**
 * 点击查看书摘笔记跳转的目录页
 *
 * @FileName: me.shurufa.activities.CatalogWithDigestActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-01 15:48
 */
public class CatalogWithDigestActivity extends BaseActivity {

    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.toolbar_divider)
    View toolbarDivider;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;
    private String bookTitle;
    private long bookId;
    private long userId;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_catalog_with_digest;
        super.onCreate(savedInstanceState);
        bookId = getIntent().getLongExtra(Constants.ARG_BOOK_ID,0L);
        userId = getIntent().getLongExtra(Constants.ARG_USER_ID,0L);
        LogUtils.kLog().e("catalog userId =" + userId);
        uiInit();
        initData();
    }

    private void initData() {
        CatalogFragment fragment = CatalogFragment.newInstance(bookId,userId);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }

    private void uiInit() {
        new RequestServerTask<BookInfoResponse>(BookInfoResponse.class) {
            @Override
            protected String requestServer() {
                Map<String, Object> params = new HashMap<>();
                params.put("bookId", bookId);
                return HttpUtil.post(NetworkConstants.GET_BOOK_INFO, params);
            }

            @Override
            protected void onSuccess(BookInfoResponse result) {
                if (result.data!=null&&!TextUtils.isEmpty(result.data.title))
                    titleTextView.setText(result.data.title);
                else
                    titleTextView.setText(getString(R.string.catalog));
            }
        }.start();
    }
}
